layui.use(['layer','table','jquery','form', 'laydate'],function(){
    let layer = layui.layer,
        table = layui.table,
        $ = layui.jquery,
        laydate = layui.laydate,
        form = layui.form;
    //日期

    laydate.render({
        elem: '#creationTime'
        , min: 0
        , max: 7
    });

    let tabInstance = table.render({
        elem: "#planList",
        url: "/draw-plan/list/"+$(".id").val(),
        page: true,
        cols: [[
            {field: 'id', title: '开发编号', width: 80, sort: true, fixed: 'left'},
            {field: 'opportunityId', title: '销售机会id'},
            {field: 'planItem', title: '计划内容',edit: 'text'},
            {field: 'founder', title: '创建人'},
            {field: 'creationTime', title: '创建时间'},
            {field: 'status', title: '状态，1为成功，0为开发，-1为失败'},
            {title: '操作', field: "right", width: 150, align: "center", toolbar: "#planTool"}
        ]]
    });
    let tabInstance2 = table.render({
        elem: "#resultList",
        url: "/draw-plan/list/"+$(".id").val(),
        page: true,
        cols: [[
            {field: 'id', title: '开发编号', width: 80, sort: true, fixed: 'left'},
            {field: 'opportunityId', title: '销售机会id'},
            {field: 'planItem', title: '计划内容',},
            {field: 'result', title: '执行效果', edit: 'text'},
            {field: 'founder', title: '创建人'},
            {field: 'creationTime', title: '创建时间'},
            {field: 'status', title: '状态，1为成功，0为开发，-1为失败'},
            {title: '操作', field: "right", width: 150, align: "center", toolbar: "#resultTool"}
        ]]
    });

    //监听工具条
    table.on('tool(planList)',function(obj){
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值

        if(layEvent === 'edit_btn') {
            // addNews(data);
            $.post('draw-plan/editPlan',data, function (info) {
                layer.msg(info.msg);
                tabInstance.reload();
            });

        }
        if(layEvent === 'del_btn') {
                layer.confirm("你确定要删除【"+data.id+"】吗？",{icon: 3,anim:4, title:'提示',skin:"layui-layer-molv"},function(){
                    $.post('draw-plan/delete/'+obj.data.id,function (data) {
                        tabInstance.reload();
                        layer.msg(data.msg);
                    })
                });
        }
    });

    //执行效果监听工具条
    table.on('tool(resultList)',function(obj){
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值
        if(layEvent === 'result_btn'){
            $.post('draw-plan/editResult',data, function (info) {
                layer.msg(info.msg);
                tabInstance2.reload();
            });
        }
    });

    //返回上一个页面
    $(".return_a1").click(function () {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
    })

    $(".help").click(function () {
        alert("请按照提示操作，以避免系统崩溃！出现问题，请与管理员zhong联系!");
    })



    //客户开发成功，添加新客户，传进客户名称，客户联系人，客户联系人电话
    $(".do_sucess").click(function () {
        var sname=document.getElementById("sname");
        var contacts=document.getElementById("contacts");
        var contactsTel=document.getElementById("contactsTel");
        let data = {};
        data['customerid']=document.getElementById("id").value;
        data['customerName']=sname.value;
        data['customerMgr']=contacts.value;
        data['phone']=contactsTel.value;
        console.log(data);
        $.post('draw-plan/saveCustomerInfo',data,function (info) {
            layer.msg(info.msg);
            tabInstance.reload();
            tabInstance2.reload();
        })
    })

    //开发客户失败，执行删除计划！
    $(".do_delete").click(function () {
        $.post('draw-plan/deleteBySId/'+$(".id").val(), function (info) {
            layer.msg(info.msg);
            tabInstance.reload();
            tabInstance2.reload();
        });
    })



    //添加计划，获取销售id，计划时间，计划内容。
    $(".saveplan").click(function () {
        var creationTime=document.getElementById("creationTime");
        var planItem=document.getElementById("planItem");
        var assignName=$(".assignName").val();
        var opportunityId=$(".id").val();
        let data = {};
        data['opportunityId']=opportunityId;
        data[planItem.name]=planItem.value;
        data['founder']=assignName;
        console.log(data['founder']);
        data[creationTime.name]=creationTime.value;
        $.post('draw-plan/savePlan',data, function (info) {
            layer.msg(info.msg);
            tabInstance.reload();
            tabInstance2.reload();
        });
    })

    //制定计划时隐藏开发界面的按钮
    $(".make_plan").click(function () {
        var tab1=document.getElementById("div1");
        var tab2=document.getElementById("div2");
        var form1=document.getElementById("save_form");
        var do_plan=document.getElementById("do_plan");
        var make_plan=document.getElementById("make_plan");
        var do_sucess=document.getElementById("do_sucess");
        var do_delete=document.getElementById("do_delete");
        var return_a1=document.getElementById("return_a1");;
        tab1.style.display="block";
        tab2.style.display="none";
        do_plan.style.display="block";
        make_plan.style.display="none";
        do_sucess.style.display="none";
        do_delete.style.display="none";
        return_a1.style.display="block";
        form1.style.display="block";
    })

    //执行计划时隐藏制定计划界面的按钮
    $(".do_plan").click(function () {
        var tab1=document.getElementById("div1");
        var tab2=document.getElementById("div2");
        var form1=document.getElementById("save_form");
        var make_plan=document.getElementById("make_plan");
        var do_sucess=document.getElementById("do_sucess");
        var return_a1=document.getElementById("return_a1");
        var do_plan=document.getElementById("do_plan");
        var do_delete=document.getElementById("do_delete");
        tab1.style.display="none";
        tab2.style.display="block";
        do_plan.style.display="none";
        make_plan.style.display="block";
        do_sucess.style.display="block";
        do_delete.style.display="block";
        form1.style.display="none";
        return_a1.style.display="none";
    })
});